function [x , globalBestFitness] = NSDE(x,popsize, maxIteration,dim, LB, UB, CR, Fun)

Sol(popsize, dim) = 0;    % 种群的初始化和计算适应度值
Fitness(popsize) = 0;
subDim = size(x,2) / dim;    % 子空间的维度
for i = 1:popsize
    Sol(i,:) = LB + (UB - LB) .* rand(1, dim);
    for i0 = 1 : dim
        ind = ((i0 - 1) * subDim + 1) : i0 * subDim;
        x(1,ind) = x(1,ind) .* Sol(i,i0);
    end
    Fitness(i) = Fun(x(1,:));
end

for time = 1:maxIteration
    for i = 1:popsize
        r = randperm(popsize, 3);  % 突变,在1~pop中随机选择5个数组成一个数组
        if rand() > 0.5
            pd = makedist('tLocationScale','mu',0,'sigma',1,'nu',1);
            F = random(pd,1,1);    %生成1个柯西随机数
        else
            F = normrnd(0.5,0.5);    %生成1个高斯随机数
        end
        mutantPos = Sol(r(1),:) + F * (Sol(r(2),:) - Sol(r(3),:));
        
        jj = randi(dim);  % 选择至少一维发生交叉
        for d = 1:dim
            if rand() < CR || d == jj
                crossoverPos(d) = mutantPos(d);
            else
                crossoverPos(d) = Sol(i,d);
            end
        end
        
        crossoverPos(crossoverPos>UB) = UB;    % 检查是否越界
        crossoverPos(crossoverPos<LB) = LB;
        
        for i1 = 1 : dim
            ind1 = ((i1 - 1) * subDim + 1) : i1 * subDim;
            x(1,ind1) = x(1,ind1) .* crossoverPos(1,i1);
        end
        evalNewPos = Fun(x(1,:));    % 将突变和交叉后的变量重新评估
        if evalNewPos < Fitness(i)    % 小于原有值就更新
            Sol(i,:) = crossoverPos;
            Fitness(i) = evalNewPos;
        end
    end

    [fbest, bestIndex] = min(Fitness);
    globalBest = Sol(bestIndex,:);
    for i2 = 1 : dim
        ind2 = ((i2 - 1) * subDim + 1) : i2 * subDim;
        x(1,ind2) = x(1,ind2) .* globalBest(1,i2);
    end
    globalBestFitness = fbest;
end
end